package com.bysj.yqsht.web.controller;


import com.bysj.yqsht.bean.Teacher;
import com.bysj.yqsht.bean.extend.TeacherExtend;
import com.bysj.yqsht.service.ITeacherService;
import com.bysj.yqsht.utils.Message;
import com.bysj.yqsht.utils.MessageUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teacher")
@Api(description = "老师管理接口")
public class TeacherController {

    @Autowired
    private ITeacherService teacherService;

    @GetMapping("/findAll")
    public Message findAll(){
        List<Teacher> list = teacherService.findAll();
        return MessageUtil.success(list);
    }
    @GetMapping("/findAllWithSubject")
    public Message findAllWithSubject(Teacher teacher, @RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "10") int limit){
        List<TeacherExtend> list = teacherService.findAllWithSubject(teacher,pageNum,limit);
        return MessageUtil.success(list);
    }

    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(Teacher teacher) {
//       判断该老师名字是否存在
//        if (teacher.getName() != null) {
//            int i = teacherService.selectDataCount(teacher);
//            if (i != 0) {
//                return MessageUtil.error("该老师姓名重复！请更改！");
//            }
            teacherService.saveOrUpdate(teacher);
            return MessageUtil.success("修改或新增成功");
//        }
//        return MessageUtil.error("该老师不存在");

    }


        @GetMapping("deleteById")
        public Message deleteById ( long id){
            teacherService.detelateById(id);
            return MessageUtil.success("删除成功");
        }
        @GetMapping("findWithAllByName")
    public Message findWithAllByName(String name,String number,Long subjectId){
        List<TeacherExtend> teacherExtends = teacherService.findWithAllByName(name,number,subjectId);
        return MessageUtil.success(teacherExtends);
        }
    }

